<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- key属性的作用：在v-for循环时保证数据的唯一性，准确性 -->
        ID:<input type="text" v-model='idModel' /> Name:
        <input type="text" v-model="nameModel">
        <input type="button" value="添加" @click="clickBtn">
        <ul>
            <!-- <li v-for="item in list">{{item.id}}-{{item.name}}</li> -->
            <li v-for="item in list" :key="item.id">
                <!-- key保证数据的唯一，打钩的数据不会出错 -->
                <input type="checkbox">{{item.id}}-{{item.name}}
            </li>
        </ul>
    </div>
</body>

</html>
<script src="js/vue3.4.21.js"></script>
<script>
    const {
        createApp,
        ref
    } = Vue

    createApp({
        setup() {
            //1、给添加按钮绑定点击事件
            //2、去除输入的id和name的值
            //3、添加到列表之中
            const list = ref([{
                id: 1,
                name: '张三'
            }, {
                id: 2,
                name: '李四'
            }])
            const idModel = ref('')
            const nameModel = ref('')

            function clickBtn() {
                console.log(idModel.value, nameModel.value);
                //向数组末尾添加元素push()
                // list.value.push({
                //     id: idModel.value,
                //     name: nameModel.value
                // })
                //向数组前面添加元素unshift()
                list.value.unshift({
                    id: idModel.value,
                    name: nameModel.value
                })
            }
            return {
                list,
                clickBtn,
                idModel,
                nameModel
            }
        }
    }).mount('#app')
</script>
<!-- https://www.cnblogs.com/bigron/p/17486819.html 设置镜像源    npm create vue@latest-->
<!-- npm install  npm run dev -->